#!/bin/bash

CHANNEL_NAME1="mychannel1"
CHANNEL_NAME2="mychannel2"
CHANNEL_NAME3="mychannel3"
CHANNEL_NAME4="mychannel4"
CHANNEL_NAME5="mychannel5"
CHANNEL_NAME6="mychannel6"
CHANNEL_NAME7="mychannel7"
CHANNEL_NAME8="mychannel8"
CHANNEL_NAME9="mychannel9"
CHANNEL_NAME10="mychannel10"
CHANNEL_NAME=("mychannel1" "mychannel2" "mychannel3" "mychannel4" "mychannel5" "mychannel6" "mychannel7" "mychannel8" "mychannel9" "mychannel10")
export FABRIC_ROOT=$PWD/bin/
export FABRIC_CFG_PATH=$PWD
echo

OS_ARCH=$(echo "$(uname -s|tr '[:upper:]' '[:lower:]'|sed 's/mingw64_nt.*/windows/')-$(uname -m | sed 's/x86_64/amd64/g')" | awk '{print tolower($0)}')

## Generates Org certs using cryptogen tool
function generateCerts (){
	CRYPTOGEN=$FABRIC_ROOT/cryptogen

	echo
	echo "##########################################################"
	echo "##### Generate certificates using cryptogen tool #########"
	echo "##########################################################"
	if [ -d "crypto-config" ]; then
    rm -Rf crypto-config
  fi
	$CRYPTOGEN generate --config=./crypto-config.yaml
	echo
}

# Using docker-compose-e2e-template.yaml, replace constants with private key file names
# generated by the cryptogen tool and output a docker-compose.yaml specific to this
# configuration
function replacePrivateKey() {
  # sed on MacOSX does not support -i flag with a null extension. We will use
  # 't' for our back-up's extension and delete it at the end of the function
  ARCH=$(uname -s | grep Darwin)
  if [ "$ARCH" == "Darwin" ]; then
    OPTS="-it"
  else
    OPTS="-i"
  fi

  # Copy the template to the file that will be modified to add the private key
  cp docker-compose-e2e-template.yaml docker-compose-e2e.yaml

  # The next steps will replace the template's contents with the
  # actual values of the private key file names for the two CAs.
  CURRENT_DIR=$PWD
  cd crypto-config/peerOrganizations/org1.example.com/ca/
  PRIV_KEY=$(ls *_sk)
  cd "$CURRENT_DIR"
  sed $OPTS "s/CA1_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
  cd crypto-config/peerOrganizations/org2.example.com/ca/
  PRIV_KEY=$(ls *_sk)
  cd "$CURRENT_DIR"
  sed $OPTS "s/CA2_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
  cd crypto-config/peerOrganizations/org3.example.com/ca/
  PRIV_KEY=$(ls *_sk)
  cd $CURRENT_DIR
  sed $OPTS "s/CA3_PRIVATE_KEY/${PRIV_KEY}/g" docker-compose-e2e.yaml
  # If MacOSX, remove the temporary backup of the docker-compose file
  if [ "$ARCH" == "Darwin" ]; then
    rm docker-compose-e2e.yamlt
  fi
}

## Generate orderer genesis block , channel configuration transaction and anchor peer update transactions
function generateChannelArtifacts() {

	CONFIGTXGEN=$FABRIC_ROOT/configtxgen
  if [ ! -d "channel-artifacts" ]; then
    mkdir "channel-artifacts"
  fi
	echo "##########################################################"
	echo "#########  Generating Orderer Genesis block ##############"
	echo "##########################################################"
	# Note: For some unknown reason (at least for now) the block file can't be
	# named orderer.genesis.block or the orderer will fail to launch!
	$CONFIGTXGEN -profile TwoOrgsOrdererGenesis -channelID e2e-orderer-syschan -outputBlock ./channel-artifacts/genesis.block

	echo
	echo "#################################################################"
	echo "### Generating channel configuration transaction 'channel.tx' ###"
	echo "#################################################################"
	for i in {1..10} ; do
	  echo Generating channel configuration transaction ${CHANNEL_NAME[$i-1]}.tx
	  $CONFIGTXGEN -profile TwoOrgsChannel${i} -outputCreateChannelTx ./channel-artifacts/${CHANNEL_NAME[$i-1]}.tx -channelID ${CHANNEL_NAME[$i-1]}
	done
#	$CONFIGTXGEN -profile TwoOrgsChannel1 -outputCreateChannelTx ./channel-artifacts/mychannel.tx -channelID $CHANNEL_NAME1


	echo
	echo "#################################################################"
	echo "#######    Generating anchor peer update for Org1MSP   ##########"
	echo "#################################################################"
	$CONFIGTXGEN -profile TwoOrgsChannel1 -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors.tx -channelID $CHANNEL_NAME1 -asOrg Org1MSP
	$CONFIGTXGEN -profile TwoOrgsChannel1 -outputAnchorPeersUpdate ./channel-artifacts/Org2MSPanchors.tx -channelID $CHANNEL_NAME1 -asOrg Org2MSP

	$CONFIGTXGEN -profile TwoOrgsChannel2 -outputAnchorPeersUpdate ./channel-artifacts/Org1MSPanchors1.tx -channelID $CHANNEL_NAME2 -asOrg Org1MSP
	$CONFIGTXGEN -profile TwoOrgsChannel2 -outputAnchorPeersUpdate ./channel-artifacts/Org3MSPanchors.tx -channelID $CHANNEL_NAME2 -asOrg Org3MSP
	echo
}

generateCerts
replacePrivateKey
generateChannelArtifacts
